home *** CD-ROM | disk | FTP | other *** search
- class SideScroller.MovingObject extends SideScroller.BasicObject
- {
- var bReactToCollisions;
- var bShouldMove;
- var nSpeedX;
- var nSpeedYRegistration;
- var nSpeedYFront;
- var nReturnRightAngleSpeed;
- var nTimeSinceLastGroundHit;
- var nCliffClimbCapacity;
- var nGroundSpeedModificator;
- var oLayer;
- var mcRef;
- static var BOUNCE_LOSS_HORIZONTAL = 8;
- static var ACCELERATION_VERTICAL = 1.2;
- static var FRICTION_HORIZONTAL = 0.4;
- static var MAX_FRONT_SPEED_VERTICAL = 2;
- static var MINIMUM_SPEED_VERTICAL = 0.2;
- static var MINIMUM_SPEED_HORIZONTAL = 0.4;
- static var ABS_MIN_SPEED = 1;
- static var RETURN_RIGHT_ANGLE_ACCELERATION = 0.5;
- static var DEFAULT_CLIFF_CLIMB_CAPACITY = 25;
- static var ANGLE_CHANGE_DIVIDER = 1;
- static var ANGLE_MINIMUM_CHANGE = 0.5;
- static var MAX_ANGLE = 65;
- function MovingObject(__mcRef, __oLayer)
- {
- super(__mcRef,__oLayer);
- this.bReactToCollisions = true;
- this.bShouldMove = true;
- this.nSpeedX = 0;
- this.nSpeedYRegistration = 0;
- this.nSpeedYFront = 0;
- this.nReturnRightAngleSpeed = 0;
- this.nTimeSinceLastGroundHit = 0;
- this.nCliffClimbCapacity = SideScroller.MovingObject.DEFAULT_CLIFF_CLIMB_CAPACITY;
- this.nGroundSpeedModificator = 0;
- this.oLayer.doAddListener(this);
- }
- function doSetRotation(__nRotation, __bRelative)
- {
- if(__bRelative == undefined)
- {
- __bRelative = false;
- }
- if(__bRelative)
- {
- this.mcRef._rotation += __nRotation;
- }
- else
- {
- this.mcRef._rotation = __nRotation;
- }
- }
- function doMove(__nSX, __nSY, __bRelative)
- {
- if(__bRelative == undefined)
- {
- __bRelative = true;
- }
- if(__bRelative)
- {
- this.doMoveHorizontalTo(this.Ref._x + __nSX);
- this.doMoveVerticalTo(this.Ref._y + __nSY);
- }
- else
- {
- this.doMoveHorizontalTo(__nSX);
- this.doMoveVerticalTo(__nSY);
- }
- }
- function doSetSpeeds(__nSX, __nSY, __bRelative)
- {
- if(__bRelative == undefined)
- {
- __bRelative = false;
- }
- if(__bRelative)
- {
- if(__nSX != undefined)
- {
- this.nSpeedX += __nSX;
- }
- if(__nSY != undefined)
- {
- this.nSpeedYRegistration += __nSY;
- }
- }
- else
- {
- if(__nSX != undefined)
- {
- this.nSpeedX = __nSX;
- }
- if(__nSY != undefined)
- {
- this.nSpeedYRegistration = __nSY;
- }
- }
- }
- function doEnterFrame()
- {
- super.doEnterFrame();
- this.doMoveBothDirections();
- this.doCheckObjects();
- }
- function doDestroy()
- {
- this.oLayer.doRemoveListener(this);
- super.doDestroy();
- }
- function get Speeds()
- {
- return {x:this.nSpeedX,y:this.nSpeedYRegistration};
- }
- function get CliffClimbCapacity()
- {
- return this.nCliffClimbCapacity;
- }
- function doMoveBothDirections()
- {
- this.doGroundAffectHorizontalSpeed();
- var _loc2_ = this.nSpeedX + this.nGroundSpeedModificator;
- if(this.bShouldMove)
- {
- if(_loc2_ < SideScroller.MovingObject.ABS_MIN_SPEED)
- {
- _loc2_ = SideScroller.MovingObject.ABS_MIN_SPEED;
- }
- }
- this.doMoveHorizontalTo(this.Ref._x + _loc2_);
- this.doMoveVerticalTo(this.Ref._y + this.nSpeedYRegistration);
- }
- function doMoveHorizontalTo(__nFuturePosition)
- {
- var _loc6_ = false;
- var _loc3_ = false;
- var _loc2_ = new Array();
- var _loc4_ = undefined;
- switch(Library.Utils.MoreMath.getPolarity(this.nSpeedX))
- {
- case 1:
- _loc4_ = "mcHitFront";
- break;
- case -1:
- _loc4_ = "mcHitBack";
- }
- for(var _loc5_ in this.mcRef.mcState)
- {
- if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
- {
- _loc2_.push(this.mcRef.mcState[_loc5_]);
- }
- }
- for(_loc5_ in _loc2_)
- {
- if(!_loc3_)
- {
- _loc3_ = this.isHitObject(_loc2_[_loc5_]);
- }
- }
- if(_loc3_ || _loc6_)
- {
- if(this.bReactToCollisions)
- {
- this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.BOUNCE_LOSS_HORIZONTAL);
- this.doReactCollision();
- this.nSpeedX *= -1;
- __nFuturePosition = this.mcRef._x + this.nSpeedX;
- }
- }
- this.mcRef._x = __nFuturePosition;
- }
- function doMoveVerticalTo(__nFuturePosition)
- {
- if(this.Speeds.y < 0)
- {
- var _loc2_ = new Array();
- var _loc4_ = "mcHitTop";
- var _loc3_ = false;
- for(var _loc5_ in this.mcRef.mcState)
- {
- if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
- {
- _loc2_.push(this.mcRef.mcState[_loc5_]);
- }
- }
- for(_loc5_ in _loc2_)
- {
- if(!_loc3_)
- {
- _loc3_ = this.isHitObject(_loc2_[_loc5_]);
- }
- }
- if(_loc3_)
- {
- this.nSpeedYRegistration = 0;
- this.nSpeedYFront = 0;
- __nFuturePosition = this.mcRef._y;
- }
- }
- var _loc6_ = false;
- var _loc8_ = this.oLayer.getFloorAt(this.Ref._x,__nFuturePosition,this);
- if(Math.floor(__nFuturePosition) >= Math.floor(_loc8_) && this.nSpeedYRegistration >= 0)
- {
- _loc6_ = true;
- this.onHitGround();
- this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.FRICTION_HORIZONTAL);
- __nFuturePosition = _loc8_;
- }
- if(!_loc6_)
- {
- this.nSpeedYRegistration += SideScroller.MovingObject.ACCELERATION_VERTICAL;
- if(Math.abs(this.nSpeedYRegistration) < SideScroller.MovingObject.MINIMUM_SPEED_VERTICAL)
- {
- this.nSpeedYRegistration = 0;
- }
- }
- else
- {
- this.nSpeedYRegistration = 0;
- }
- this.mcRef._y = __nFuturePosition;
- this.doAdjustToGround(_loc6_);
- }
- function doAdjustToGround(__bRegistrationHitGround)
- {
- var _loc3_ = Library.Utils.MoreMath.getBoundsCenter(this.mcRef.mcState.mcPointRear.getBounds(this.oLayer.Ref));
- var _loc7_ = this.oLayer.getFloorAt(_loc3_.x,_loc3_.y,this);
- var _loc5_ = undefined;
- var _loc6_ = undefined;
- if(_loc7_ > _loc3_.y)
- {
- if(__bRegistrationHitGround)
- {
- this.nSpeedYFront += SideScroller.MovingObject.ACCELERATION_VERTICAL;
- }
- else
- {
- this.nSpeedYFront = 0;
- }
- _loc5_ = _loc3_.y + this.nSpeedYFront;
- _loc6_ = false;
- }
- else
- {
- _loc5_ = _loc7_;
- _loc6_ = true;
- this.nSpeedYFront = 0;
- }
- var _loc2_ = undefined;
- _loc2_ = Math.round(Library.Utils.MoreMath.getAngle(Math.round(_loc3_.x),Math.round(_loc5_),Math.round(this.mcRef._x),Math.round(this.mcRef._y)));
- if(!__bRegistrationHitGround && !_loc6_)
- {
- this.nTimeSinceLastGroundHit = this.nTimeSinceLastGroundHit + 1;
- if(this.nTimeSinceLastGroundHit > 5)
- {
- this.nReturnRightAngleSpeed += SideScroller.MovingObject.RETURN_RIGHT_ANGLE_ACCELERATION;
- _loc2_ = Library.Utils.MoreMath.getReachZero(_loc2_,this.nReturnRightAngleSpeed);
- }
- }
- else
- {
- this.nReturnRightAngleSpeed = 0;
- this.nTimeSinceLastGroundHit = 0;
- }
- if(Math.abs(_loc2_) > SideScroller.MovingObject.MAX_ANGLE)
- {
- _loc2_ = Library.Utils.MoreMath.getPolarity(_loc2_) * SideScroller.MovingObject.MAX_ANGLE;
- }
- var _loc8_ = _loc2_ - this.mcRef._rotation;
- var _loc4_ = _loc8_ / SideScroller.MovingObject.ANGLE_CHANGE_DIVIDER;
- if(Math.abs(_loc4_) < SideScroller.MovingObject.ANGLE_MINIMUM_CHANGE)
- {
- _loc4_ = 0;
- }
- this.mcRef._rotation += _loc4_;
- }
- function doGroundAffectHorizontalSpeed()
- {
- var _loc2_ = 0;
- switch(Library.Utils.MoreMath.getPolarity(this.mcRef._rotation))
- {
- case 1:
- _loc2_ = 1;
- break;
- case -1:
- _loc2_ = -1;
- }
- this.nGroundSpeedModificator = _loc2_ * (Math.pow(Math.abs(this.mcRef._rotation),1.2) * 0.051);
- }
- function isHitObject(__mcHitReference)
- {
- var _loc4_ = Library.Utils.MoreMath.getBoundsCenter(__mcHitReference.getBounds(this.oLayer.Ref));
- var _loc3_ = this.oLayer.getCollidableObjects(this);
- var _loc6_ = this.oLayer.Ref._x;
- var _loc7_ = this.oLayer.Ref._y;
- var _loc5_ = false;
- var _loc2_ = undefined;
- _loc2_ = 0;
- while(_loc2_ <= _loc3_.length - 1)
- {
- if(_loc3_[_loc2_].Hit.hitTest(_loc4_.x + _loc6_,_loc4_.y + _loc7_,true))
- {
- _loc5_ = true;
- _loc3_[_loc2_].onHit(this);
- }
- _loc2_ = _loc2_ + 1;
- }
- return _loc5_;
- }
- function onHitGround()
- {
- }
- function doReactCollision()
- {
- }
- }
-